package com.ecar.api.service.common.impl;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecar.api.service.common.ExceptionHandleService;
import com.ecar.api.support.util.UuidUtils;
import com.ecar.common.dto.DataBean;
import com.ecar.common.util.JSONUtil;
import com.ecar.core.driver.dao.ExceptionHandleMapper;
import com.ecar.core.driver.entity.ExceptionHandle;
import com.ecar.core.order.dao.OrderMapper;
import com.ecar.core.order.entity.Order;
@Service
@Transactional
public class ExceptionHandleServiceImpl implements ExceptionHandleService{
	
	@Autowired
	ExceptionHandleMapper exceptionHandleMapper;
	
	@Autowired
	OrderMapper OrderMapper;
	
	@Override
	public List<ExceptionHandle> selectAll() {
		return exceptionHandleMapper.selectAll();
	}
	
	@Override
	public int deleteByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public DataBean insert(JSONObject fields) {
		ExceptionHandle exceptionHandle = JSONUtil.deserialize(fields, ExceptionHandle.class);
		Order order=OrderMapper.selectByOrderNum(exceptionHandle.getOrderId());
		if(order==null){
			return new DataBean(403,new JSONObject(),"该订单不存在");
		}
		if(!order.getDriverId().equals(exceptionHandle.getDriverId())){
			return new DataBean(404,new JSONObject(),"司机无权访问该订单");
		}
		exceptionHandle.setId(UuidUtils.getUUID());
		exceptionHandle.setCreateTime(new Date());
		int i=exceptionHandleMapper.insert(exceptionHandle);
		if(i<=0){
			return new DataBean(403,new JSONObject(),"订单异常申请失败");
		}else{
			return new DataBean(200,new JSONObject(),"订单异常申请成功");
		}
	}

	@Override
	public int insertSelective(ExceptionHandle record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ExceptionHandle selectByPrimaryKey(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(ExceptionHandle record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateByPrimaryKey(ExceptionHandle record) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
}
